<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 项目接口相关
 */
class Centralcontact extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 处理更新投资方地区
     */
    public function area(){
        //处理数据
        //获取需要处理的数据
        $arr = Db::query("select name,count(name) as count,investment_area from nsig_centralcontact group by name having count(*)>1");
        $data = [];
        foreach($arr as $k => $v){
            $data[] = Db::name('centralcontact')->where(['name'=>$v['name']])->order('id')->field('id,name,investment_area')-> select();
        }
        if(!empty($data)&&is_array($data))
        {
            foreach($data as $key => $val){
                $areas = array_unique(array_filter(array_column($val,'investment_area')));
                if(count($areas) == 1) {
                    foreach ($val as $ky => $value) {
                        if (!empty($value['investment_area'])) {
                            $where1 = "name='" . trim($value['name']) . "' and (investment_area='' or investment_area is null)";
                            $info = Db::name('centralcontact')->where($where1)->find();
                            if (empty($info['investment_area'])) {
                                Db::name('centralcontact')->where($where1)->update(['investment_area' => $value['investment_area']]);
                                break;
                            }
                        }
                    }
                }
            }
        }
        // pr($data);
        // exit('更新成功');
    }


}